package com.github.yeecode.mybatisdemo.reflect;

import com.github.yeecode.mybatisdemo.model.User;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.ibatis.reflection.Reflector;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DemoApplicationCP3 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        User target = new User();
       /* getField用于返回一个指定名称的属性，但是这个属性必须是公有的，这个属性可以在父类中定义。如果是私有属性或者是保护属性，那么都会抛出异常提示找不到这个属性*/
        //Field field = User.class.getField("schoolName");
        Field field = User.class.getDeclaredField("schoolName");
        field.setAccessible(true);
        field.set(target,  "家里蹲大学");
        Object schoolName = field.get(target);
        System.out.println(schoolName);


        Field[] fields = User.class.getDeclaredFields();
        for (int i = 0; i <fields.length; i++) {
            fields[i].setAccessible(true);
            Object obj = fields[i].get(target);
            System.out.println(obj);

        }


        System.out.println(target.toString());

        Method[] declaredMethods = User.class.getDeclaredMethods();
        for (Method method: declaredMethods) {
            method.setAccessible(true);
            //如果是setter 方法, 给个参数, 如果是getter 不给参数;
            //  method.invoke(target, null);
        }

        Method toStringMethod = User.class.getDeclaredMethod("toString", null);
        Object invoke = toStringMethod.invoke(target, null); //第一个参数是对象, 剩下的是实参
        if (invoke instanceof  String){
            System.out.println(invoke);
        }

    }






}
